package light.mvc.service.patient.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.register.Tregister;
import light.mvc.page.base.PageFilter;
import light.mvc.page.patient.Register;
import light.mvc.service.patient.RegisterSeqServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RegisterSeqServiceImpl implements RegisterSeqServiceI {

	@Autowired
	private BaseDaoI<Tregister> registerDao;

	@Override
	public void add(Register register) {
		Tregister t = new Tregister();
		BeanUtils.copyProperties(register, t);
		registerDao.save(t);
	}

	@Override
	public void delete(Integer id) {
		Tregister t = registerDao.get(Tregister.class, id);
		registerDao.delete(t);
	}

	@Override
	public void edit(Register register) {
		Tregister t = registerDao.get(Tregister.class,register.getId());
		BeanUtils.copyProperties(register, t);
		registerDao.update(t);
	}

	@Override
	public Register get(Integer id) {
		Tregister t = registerDao.get(Tregister.class,id);
		Register register = new Register();
		BeanUtils.copyProperties(t, register);
		
		return register;
	}
	
	@Override
	public List<Register> dataGrid(Register registerSome, PageFilter ph) {
		List<Register> registers = new ArrayList<Register>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Tregister t ";
		List<Tregister> list = registerDao.find(hql + orderHql(ph), params, ph.getPage(), ph.getRows());
		for (Tregister t : list) {
			Register register = new Register();
			BeanUtils.copyProperties(t, register);
			registers.add(register);
		}
		return registers;
	}
	
	@Override
	public Long count(Register register, PageFilter ph) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Tregister t ";
		return registerDao.count("select count(*) " + hql, params);
	}

	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}
	
	public List<Register> getPatientsByDoctorId(Integer doctorId,Date timeSegment) {
		Map<String,Object> params = new HashMap<String,Object>();
		if(doctorId != null) {
			params.put("doctorId", doctorId);
		}
		
		params.put("timeSegment", timeSegment);
		
		String hql = "select t from Tregister t join t.doctor doctor where t.timeSegment = :timeSegment ";
		if(doctorId != null) {
			hql += "and doctor.id = :doctorId";
		}
		List<Tregister> list = registerDao.find(hql, params);
		List<Register> registers = new ArrayList<Register>();
		
		for(Tregister t:list) {
			Register register = new Register();
			register.setId(t.getId());
			register.setSeq(t.getSeq());
			register.setPatientId(t.getPatient().getId());
			register.setPatientName(t.getPatient().getName());
			if(t.getType() != 1) {
				register.setType(0);
				register.setReserveTime(t.getReserveTime());
			} else {
				register.setType(1);
			}
			register.setRegisterTime(t.getRegisterTime());
			registers.add(register);
		}
		
		return registers;	
	}
}
